<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加类别</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="父级名称" prop="parentId">
        <el-select v-model="ruleForm.parentId" placeholder="请选择">
          <el-option
                  v-for="item in parentList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="关键字列表" prop="keywords">
        <el-input v-model="ruleForm.keywords"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item label="是否启动">
        <el-switch v-model="ruleForm.isEnabled"></el-switch>
      </el-form-item>
      <el-form-item label="是否显示在导航栏">
        <el-switch v-model="ruleForm.isNav"></el-switch>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        ruleForm: {
          name: '',
          keywords:"",
          sort: 0,
          isEnabled:true,
          isNav:true,
          parentId:""
        },
        parentList: [{
          id: 1,
          name: '系统管理员'
        }],
        rules: {
          name: [
            {required: true, message: '请输入类别名称', trigger: 'blur'},
            {min: 2, max: 25, message: '长度在 2 到 25 个字符', trigger: 'blur'}
          ],
          description: [
            {required: true, message: '请输入类别简介', trigger: 'blur'},
            {min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur'}
          ]
        }
      };
    },
    methods: {
      loadParentList(){
        let url = "http://localhost:9080/category/listByParentId?id=0";
        let formData = this.qs.stringify(this.ruleForm);
        this.axios
                .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
                .post(url, formData).then((response) =>{
                  let responseBody = response.data;
                  this.parentList = responseBody.data;
                  this.value = responseBody.data;
        })
      },
      handleChange(){

      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let url = "http://localhost:9080/category/add-new";
            this.axios.post(url,this.ruleForm).then((response)=>{
              let responseBody = response.data;
              if(responseBody.state==20000){
                this.$message({
                  message:"类别添加成功",
                  type: "success"
                });
                this.resetForm(formName);
              }else{
                this.$message.error(responseBody.message);
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    created(){
      console.log("运行loadParentList()")
      this.loadParentList();
    }
  }
</script>

<style scoped>

</style>